package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.*;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.AllProductRequest;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.RecommendResponse;
import com.cskaoyan.shopping.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author LY
 * @since 2022/06/26 20:57
 */
@Slf4j
@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ProductConverter productConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse response = new ProductDetailResponse();
        try {
            log.info("IProductServiceImpl.getProductDetail 验证request参数");
            request.requestCheck();
            log.info("IProductServiceImpl.getProductDetail request参数验证通过");
            Long productId = request.getId();
            log.info("IProductServiceImpl.getProductDetail itemMapper");
            Item item = itemMapper.selectByPrimaryKey(productId);
            log.info("IProductServiceImpl.getProductDetail itemDescMapper");
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(productId);

            if(item == null || itemDesc == null){
                log.error("商品不存在：{}", productId);
                throw new BizException(productId + "商品不存在");
            }
            log.info("IProductServiceImpl.getProductDetail: 商品存在 {}", productId);
            ProductDetailDto productDetailDto = new ProductDetailDto();
            productDetailDto.setProductId(productId);
            productDetailDto.setSalePrice(item.getPrice());
            productDetailDto.setProductName(item.getTitle());
            productDetailDto.setSubTitle(item.getSellPoint());
            productDetailDto.setLimitNum(Long.decode(item.getLimitNum().toString()));
            productDetailDto.setProductImageBig(item.getImageBig());
            productDetailDto.setDetail(itemDesc.getItemDesc());
            String[] images = item.getImages();
            List<String> list = Arrays.asList(images);
            productDetailDto.setProductImageSmall(list);

            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            response.setProductDetailDto(productDetailDto);
        }catch (Exception e){
            log.error("IProductServiceImpl.getProductDetail Occur Exception :" + e);
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        return null;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        return null;
    }

    @Override
    public AllItemResponse getAllItems() {
        AllItemResponse response = new AllItemResponse();

        try {
            List<Item> items = itemMapper.selectAll();

            List<ItemDto> itemDtos = productConverter.items2ItemDtos(items);

            response.setAllItems(itemDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            log.error("IProductServiceImpl.getAllItems Occur Exception :" + e);
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }

        return response;
    }
}
